﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using System.Runtime.InteropServices;
using System.Linq;
using DevExpress.XtraBars;
using DevExpress;

namespace QuanLySieuThi_08520114_08520220
{
    public partial class ThanhToan : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private controller_hoadon controller;
        string ma_pttt = ""; 
        public ThanhToan()
        {
            InitializeComponent();
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("McSkin");
            controller = new controller_hoadon();
            list_data();
        }

       private void list_data()
        {
            DataTable data = controller.list_pttt();
            if (data != null)
            {
                try
                {

                    ListView.DataSource = data;
                }
                catch (SystemException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void ThoatBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void XoaBtn_Click(object sender, EventArgs e)
        {
            if (List.GetFocusedDataSourceRowIndex() < 0)
                return;

            if (MessageBox.Show("Bạn có thực sự muốn xoá ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                return;

            try{
                if (controller.del_pttt(List.GetRowCellValue(List.GetFocusedDataSourceRowIndex(), "MaPhuongThucTT").ToString()))
                {
                    MessageBox.Show("Bạn đã xoá thành công !", "Thông báo");
                }
            }
            catch(SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
            TenTb.Text = "";                    

            this.list_data();
        }

        private void ThemBtn_Click(object sender, EventArgs e)
        {
            if (TenTb.Text == "")
            {
                MessageBox.Show("Bạn chưa điền tên phương thức thanh toán !", "Thông báo");
                return;
            }

            try
            {
                if (controller.add_pttt(TenTb.Text))
                {
                    this.list_data();
                    TenTb.Text = "";
                    
                }
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }

            ma_pttt = "";
        }

        private void SuaBtn_Click(object sender, EventArgs e)
        {
            if (TenTb.Text == "" || ma_pttt == "")
                return;

            if (MessageBox.Show("Bạn có thực sự muốn sửa ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                return;


            try
            {
                if (controller.update_pttt(ma_pttt,TenTb.Text))
                {
                    MessageBox.Show("Bạn đã sửa thành công !", "Thông báo");

                }
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }

            TenTb.Text = "";
            ma_pttt = "";
            this.list_data();
        }

        private void ListView_DoubleClick(object sender, EventArgs e)
        {
            TenTb.Text = List.GetRowCellValue(List.GetFocusedDataSourceRowIndex(), "TenPhuongThucTT").ToString();
            ma_pttt = List.GetRowCellValue(List.GetFocusedDataSourceRowIndex(), "MaPhuongThucTT").ToString();
        }

    }
}